package com.shop.service.impl;

import com.shop.constant.Constants;
import com.shop.dao.UserDao;
import com.shop.dao.impl.UserDaoImpl;
import com.shop.entity.User;
import com.shop.exception.*;
import com.shop.factory.ObjectFactory;
import com.shop.service.UserService;
import com.shop.transaction.TransactionManager;

public class UserServiceImpl implements UserService {

    private UserDao dao = (UserDaoImpl) ObjectFactory.getInstance(Constants.USER_DAO_KEY);

    /**
     * 用户注册
     * @param user
     * @return
     * @throws DAOException
     */
    @Override
    public String register(User user) throws DAOException, UserExistException, UserNotExistException {

        TransactionManager tm = new TransactionManager();
        String img1 = null;
        int result = 0;
        try {
            //开启事务
            tm.openTransaction();
            //下面是业务处理代码
            //验证用户是否存在
            User user1 = dao.selectUserByName(user.getU_name());
            if(user1 != null) {
                throw new UserExistException("用户已经存在!");
            }
            result = dao.register(user); //注册用户
            if(result == 0) {
                tm.rollback();
                throw new DAOException();
            }
            User user2 = dao.selectUserByName(user.getU_name());//先查询用户
            if(user2 == null) {
                throw new UserNotExistException("用户不存在!");
            }
            result = dao.createCart(user2.getU_id()); //创建用户的购物车
            if(result == 0) {
                tm.rollback();
                throw new DAOException();
            }
            //业务处理代码写完后需要提交
            tm.commit();
        } catch (DAOException e) {
            //如果报错，这里需要回滚
            tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
        return null;
    }

    /**
     * 用户登录
     * @param name
     * @param pwd
     * @return
     * @throws UserNotExistException
     * @throws DAOException
     */
    @Override
    public User login(String name, String pwd) throws UserNotExistException, DAOException {
        User user = dao.userRex(name,pwd);
        if(user == null) {
            throw new UserNotExistException("用户不存在!");
        }
        return user;
    }

    //个人中心界面
    @Override
    public User selectUserInformationById(int user_id) throws UserNotExistException, DAOException {

        User user = null;
        user = dao.selectUserById(user_id);
        if(user == null) {
            throw new UserNotExistException("用户不存在");
        }

        return user;
    }

    @Override
    public void updateUserInformation(User user, int u_id) throws UserInformationChangeException, DAOException {
        int result = 0;
        result = dao.updateUserInformation(user,u_id);
        if(result == 0) {
            throw new UserInformationChangeException("用户信息修改失败");
        }
    }

    @Override
    public void changeUserHeadImg(String id, String url) {
        User user = new User();
        user.setU_id(Integer.parseInt(id));
        user.setU_img(url);
        try {
            dao.updateUserImg(user);
        } catch (DAOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void changePwd(String old_pwd, String new_pwd, String a_new_pwd, String u_id) throws changePwdException, DAOException {
        TransactionManager tm = new TransactionManager();
        try {
            int result = dao.pwdRex(u_id,old_pwd);
            if(result == 0) {
                throw new changePwdException("旧密码有误");
            }
            tm.openTransaction();
            result = dao.changePwd(new_pwd,u_id);
            tm.commit();
        } catch (DAOException e) {
            tm.rollback();
            e.printStackTrace();
        }
    }

}
